package com.gaogzhen.config.sms;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Administrator
 * @version 1.0
 * @description sms短信发送
 * @date 2022-10-04 12:50
 */
@Configuration
public class SmsAutoConfiguration {

    /**
     * 阿里云短信服务账户accessKey
     */
    @Value("${spring.cloud.alicloud.access-key}")
    private String accessKey;

    /**
     * 阿里云短信服务账户accessKey
     */
    @Value("${spring.cloud.alicloud.secret-key}")
    private String secretKey;

    /**
     * 阿里云短信服务endpoint
     */
    @Value("${spring.cloud.alicloud.sms.endpoint}")
    private String endpoint;
//
//    /**
//     * 阿里云短信服务签名
//     */
//    @Value("${spring.cloud.alicloud.sms.signName}")
//    private String signName;


    @Bean
    public AliSms aliSms() {
        return new AliSms(createClient(), sendSmsRequest());
    }

    private SendSmsRequest sendSmsRequest() {
        return new SendSmsRequest();
    }

    private Client createClient(){
        Config config = new Config()
                // 您的 AccessKey ID
                .setAccessKeyId(accessKey)
                // 您的 AccessKey Secret
                .setAccessKeySecret(secretKey);
        // 访问的域名
        config.endpoint = endpoint;
        Client client = null;
        try {
            client = new Client(config);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("创建阿里客户端失败！");
        }

        return client;
    }

}
